package com.atguigu.exam.service.impl;

import com.atguigu.exam.entity.AnswerRecord;
import com.atguigu.exam.entity.BaseEntity;
import com.atguigu.exam.entity.ExamRecord;
import com.atguigu.exam.entity.Paper;
import com.atguigu.exam.mapper.ExamRecordMapper;
import com.atguigu.exam.mapper.PaperMapper;
import com.atguigu.exam.service.AnswerRecordService;
import com.atguigu.exam.service.ExamRecordService;
import com.atguigu.exam.vo.ExamRankingVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 考试记录Service实现类
 * 实现考试记录相关的业务逻辑
 */
@Service
public class ExamRecordServiceImpl extends ServiceImpl<ExamRecordMapper, ExamRecord> implements ExamRecordService {

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private AnswerRecordService answerRecordService;
    @Autowired
    private ExamRecordMapper examRecordMapper;

    /**
     * 分页查询考试记录
     *
     * @param iPage
     * @param studentName
     * @param status
     * @param startDate
     * @param endDate
     */
    @Override
    public void getExamRecords(Page<ExamRecord> iPage, String studentName, Integer status, String startDate, String endDate) {
        //条件拼接
        LambdaQueryWrapper<ExamRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!ObjectUtils.isEmpty(studentName), ExamRecord::getStudentName, studentName);
        //转换状态Integer->String
        if (!ObjectUtils.isEmpty(status)) {
            String s = switch (status) {
                case 0 -> "进行中";
                case 1 -> "已完成";
                case 2 -> "已批阅";
                default -> "";
            };
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(s), ExamRecord::getStatus, s);
        }
        lambdaQueryWrapper.ge(!ObjectUtils.isEmpty(startDate), ExamRecord::getStartTime, startDate);
        lambdaQueryWrapper.le(!ObjectUtils.isEmpty(endDate), ExamRecord::getStartTime, endDate);


        //分页查询
        page(iPage, lambdaQueryWrapper);

        //获取所有考试记录的试卷id
        List<Integer> ids =
                iPage.getRecords().stream().map(ExamRecord::getExamId).collect(Collectors.toList());

        LambdaQueryWrapper<Paper> paperLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paperLambdaQueryWrapper.in(!ObjectUtils.isEmpty(ids), Paper::getId, ids);
        List<Paper> papers = paperMapper.selectList(paperLambdaQueryWrapper);
        Map<Long, Paper> paperMap = papers.stream().collect(Collectors.toMap(Paper::getId, p -> p));
        //遍历为每条数据赋paper信息
        iPage.getRecords().forEach(examRecord -> examRecord.setPaper(paperMap.get(examRecord.getExamId().longValue())));

    }

    /**
     * 删除考试记录
     *
     * @param id
     */
    @Override
    public void deleteExamRecord(Integer id) {
        ExamRecord examRecord = getById(id);
        if (Objects.equals(examRecord.getStatus(), "进行中")) {
            throw new RuntimeException("考试正在进行中，无法删除");
        }
        //删除考试记录
        removeById(Long.valueOf(id));
        //删除答题记录
        answerRecordService.remove(new LambdaQueryWrapper<AnswerRecord>().eq(AnswerRecord::getExamRecordId, id));

    }

    /**
     * 获取考试排行榜
     *
     * @param paperId
     * @param limit
     * @return
     */
    @Override
    public List<ExamRankingVO> getExamRanking(Integer paperId, Integer limit) {
        //调用mapper进行查询
        List<ExamRankingVO> list = examRecordMapper.getExamRanking(paperId, limit);
        //返回结果
        return list;
    }
}